
{
Function:       _TickRate

Purpose:        Calculates the tick rate (ticks per second), averaged over Period seconds.

Author:         Mark Krisburg (MarkSanDiego)

Updated:        06/29/08    original version
}

external method: "ELTicToc.dll", int, "ELTIC", LPSTR ;        // ELTicToc timer must be installed for this function to work
external method: "ELTicToc.dll", int, "ELTOC", LPSTR ;

inputs:
    Period(NumericSimple);                                    // Number of seconds over which tick rate is averaged
    
vars:
    int j(0),                                                 // loop counter
    intrabarpersist TickInterval(0),                          // seconds between current tick and last tick
    intrabarpersist ElapsedSeconds(0),                        // seconds elapsed since indicator activated
    intrabarpersist MinValidTickTime(0),                      // minimum elapsed time for which a tick is still valid (not expired)
    intrabarpersist SecondInPeriod(0),                        // second number within period (ex. second 1, 2, 3...etc. in a 60 second period)
    intrabarpersist TicksInPeriod(0),                         // total number of ticks in most recent Period seconds
    intrabarpersist TickRate(0),                              // number of ticks per second averaged over Period seconds
    intrabarpersist TickSum(0),                               // total sum of ticks to date
    intrabarpersist Timer1(0);                                // measures time interval between ticks in seconds

arrays: 
    intrabarpersist TimeArray[](0), 
    intrabarpersist CountArray[](0);

once begin
    Timer1 = ELTic("TickInterval");
    Array_SetMaxIndex(TimeArray, Period);
    Array_SetMaxIndex(CountArray, Period);
end;

Value1 = Ticks ;                                                            // force RadarScreen to update every tick. 

if barstatus(1) = 1 then begin                                              // run only during market activity
         
    TickSum = TickSum + 1; 
    Timer1 = ELToc("TickInterval");
    TickInterval = Timer1/1000000;                                          // convert tick interval from microseconds to seconds
    Timer1 = ElTic("TickInterval");                                         // reset microsecond timer after each tick
    
    ElapsedSeconds = ElapsedSeconds + TickInterval;
    SecondInPeriod = Floor(mod(ElapsedSeconds, Period) + 1);                // number of seconds into period for current time

    if ElapsedSeconds - TimeArray[SecondInPeriod] > Period then begin       // count has expired
        CountArray[SecondInPeriod] = 1;                                     // restart count
        TimeArray[SecondInPeriod] = ElapsedSeconds;                         // record restart time
    end else begin                                                          // count has not expired  
        CountArray[SecondInPeriod] = CountArray[SecondInPeriod] + 1;        // add tick to count
        if TimeArray[SecondInPeriod] = 0 then 
            TimeArray[SecondInPeriod] = ElapsedSeconds; 
    end;
 
    TicksInPeriod = 0;
    MinValidTickTime = MaxList(ElapsedSeconds - Period, 0);
    for j = 1 to Period begin                                                // sum tick counts if not expired
        if TimeArray[j] > MinValidTickTime then 
            TicksInPeriod = TicksInPeriod + CountArray[j];
    end;
    if ElapsedSeconds > 1 then 
         TickRate = TicksInPeriod/MinList(ElapsedSeconds, Period);    
     
end;

_TickRate = TickRate;


